package Lee_code;

public class Lee_1971 {
    int [] father;
    public boolean validPath(int n, int[][] edges, int source, int destination)
    {
        father = new int[n];
        init();
        for(int i=0; i< edges.length; i++)
        {
            join(edges[i][0], edges[i][1]);
        }
        return isSame(source, destination);
    }

    //纯几把初始化
    private void init() {
        for (int i=0; i< father.length; i++)
        {
            father[i] = i;
        }
    }
    public int find(int u)
    {
        if (u==father[u])
            return u;
        else {
            father[u] = find(father[u]);
            return father[u];
        }
    }

    public boolean isSame(int u, int v)
    {
        u = find(u);
        v = find(v);
        return u==v;
    }

    public void join(int u, int v)
    {
        u = find(u);
        v = find(v);
        if(u == v)
            return;
        else father[v]=u;
    }
}
